package com.gengzp.mq.server.controller;

import com.gengzp.mq.common.model.base.MqPaginationResult;
import com.gengzp.mq.common.model.base.MqResult;
import com.gengzp.mq.common.model.req.*;
import com.gengzp.mq.common.model.resp.PageGengzpMqTopicResp;
import com.gengzp.mq.common.model.resp.SearchGengzpMqConsumerResp;
import com.gengzp.mq.common.utils.GengzpMqAssertUtils;
import com.gengzp.mq.server.service.IGengzpMqTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName GengzpMqTopicController
 * @Description 消息队列服务端topic管理控制层
 * @Author gengzp
 * @Date 2025/7/10 10:04
 */
@RestController
@RequestMapping("/gengzp/mq/topic")
public class GengzpMqTopicController {

    @Autowired
    private IGengzpMqTopicService gengzpMqTopicService;

    /**
     * 创建topic
     */
    @PostMapping("/add")
    public MqResult<Void> addTopic(@RequestBody AddGengzpMqTopicReq req) {
        GengzpMqAssertUtils.isNotBlank(req.getTopic(), "topic不能为空");
        gengzpMqTopicService.addTopic(req);
        return MqResult.getSuccessResult();
    }

    /**
     * 编辑topic
     */
    @PostMapping("/edit")
    public MqResult<Void> editTopic(@RequestBody EditGengzpMqTopicReq req) {
        GengzpMqAssertUtils.nonNull(req.getId(), "id不能为空");
        gengzpMqTopicService.editTopic(req);
        return MqResult.getSuccessResult();
    }

    /**
     * 根据id删除topic
     */
    @PostMapping("/delete_by_id")
    public MqResult<Void> deleteById(@RequestBody SearchGengzpMqInfoByIdReq req) {
        GengzpMqAssertUtils.nonNull(req.getId(), "id不能为空");
        gengzpMqTopicService.deleteTopicById(req.getId());
        return MqResult.getSuccessResult();
    }

    /**
     * 分页查询topic
     */
    @PostMapping("/page")
    public MqResult<MqPaginationResult<PageGengzpMqTopicResp>> pageQueryTopic(@RequestBody PageGengzpMqTopicReq req) {
        return MqResult.getSuccessResult(gengzpMqTopicService.pageQueryTopic(req));
    }

    /**
     * 查询topic名列表, 用于前端下拉框展示
     */
    @PostMapping("/search_topic_names")
    public MqResult<List<String>> searchTopicNames() {
        return MqResult.getSuccessResult(gengzpMqTopicService.searchTopicNames());
    }

    /**
     * 根据topic名, 查询订阅的消费者信息
     */
    @PostMapping("/search_consumer")
    public MqResult<SearchGengzpMqConsumerResp> searchConsumer(@RequestBody SearchGengzpMqConsumerReq req) {
        GengzpMqAssertUtils.isNotBlank(req.getTopic(), "topic不能为空");
        return MqResult.getSuccessResult(gengzpMqTopicService.searchConsumer(req.getTopic()));
    }

}
